
package jp.isisredirect.adkechoback;

import java.nio.ByteBuffer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;


import jp.isisredirect.adk.ADKBaseActivity;

public class AdkEchobackActivity extends ADKBaseActivity implements OnClickListener {
    static final String TAG = "AdkEchobackActivity";
    static final String ACTION_USB_PERMISSION = "jp.isisredirect.adkechoback.action.USB_PERMISSION";

    EditText inputtextfield = null;
    TextView echobacktext = null;
    Button buttonsend = null;

    public AdkEchobackActivity() {
        super(ACTION_USB_PERMISSION);
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        inputtextfield = (EditText) findViewById(R.id.InputText);
        echobacktext = (TextView) findViewById(R.id.textViewEchoback);
        buttonsend = (Button)findViewById(R.id.buttonsend);;
        buttonsend.setOnClickListener(this);
        
    }

    // ADK connection events
    @Override
    protected void onADKConnected() {
        if (echobacktext != null) {
            echobacktext.setText("connected");
            echobacktext.append("\n");
        }
    }
    @Override
    protected void onADKDisconnected() {
        if (echobacktext != null) {
            echobacktext.append("disconnected");
        }
    }

    // ADK data receive event
    @Override
   protected void onADKReceived(ByteBuffer buffer) {
        if (echobacktext != null) {
            if (0 < buffer.limit()) {
                byte[]   b = new byte[buffer.limit()];
                buffer.flip();
                buffer.get(b);
                String  s = new String(b,0,buffer.limit());
                //Log.d(TAG, "onReceived " + s );
                //echobacktext.setText("received :" + s);
                echobacktext.append(s);                
                echobacktext.append("\n");
            }
        }
    }

    // for control
    @Override
    protected void setContentViewByRes() {
        setContentView(R.layout.main);
    }

    public void onClick(View v) {
        if (v == buttonsend) {
            String  s = inputtextfield.getText().toString();
            //Log.d(TAG, "onClick " + s );
            byte[] b = s.getBytes();
            ByteBuffer  buffer = ByteBuffer.wrap(b);
            buffer.put(b);
            this.sendADKData(buffer);
        }
    }
 
}
